package com.pms.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pms.entity.SysUser;
import com.pms.service.impl.SysUserService;
import com.pms.utils.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class SysUserAction {
    private Log log = LogFactory.getLog(SysUserAction.class);
    @Autowired
    private SysUserService sysUserService;
    
    /* 旧版，单纯用PageHelper
    @RequestMapping("/data")
    public ModelAndView data(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer pageSize){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("user/data");
        PageHelper.startPage(page, pageSize);
        List<SysUser> list = sysUserService.findPage();
        PageInfo pages = new PageInfo(list);
        mv.addObject("list", list);
        mv.addObject("page", pages);
        return mv;
    }*/
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("user/edit");
        mv.addObject("user", sysUserService.findById(id));
        return mv;
    }
    @RequestMapping("/update")
    public String update(SysUser user, HttpServletRequest request){
        log.info("**** update ： " + user);
        String msg = "更新失败";
        if (sysUserService.update(user)){
            msg = "更新成功";
        }
        request.setAttribute("msg", msg);
        return "redirect:data";
    }
    //分页查询，返回更新结果
    @RequestMapping("/data")
    public String data(){
        return "user/data";
    }

    //分页查询，返回更新结果
    @RequestMapping(value = "/dataTable")
    @ResponseBody
    public DataTableResponse dataTable(@RequestBody DataTableRequest data) {
        log.info("********** test2 : " + data);
        return sysUserService.findDataPage(data);
    }
}
